<?php
/**
 * Kbs
 *
 * Project index
 * php version : 5.2.6
 *
 * @author      kim
 * @version     1.0
 */

/**
 * Php version checking
 *
 */
if (true === version_compare(phpversion(), '5.2.4', '<')) {
    exit('Boot failure ! (102)');
}

/**
 * Define path to project root
 *
 */
defined('PROJECT_ROOT')
    || define('PROJECT_ROOT',
              realpath(dirname(dirname(__FILE__))));

/**
 * Define path to application directory
 *
 */
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH',
              PROJECT_ROOT . '/application');

/**
 * Define application environment
 *
 */
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV',
              (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV')
                                         : 'production'));

/**
 * Set include paths
 *
 * Typically, you will also want to add your library/ directory
 * to the include_path, particularly if it contains your ZF install
 */
set_include_path(implode(PATH_SEPARATOR, array(
    PROJECT_ROOT . '/library'
)));

/**
 * Config file inc
 *
 * Caching file
 */
defined('CONFIG_INC')
    || define('CONFIG_INC',
              PROJECT_ROOT . '/library/Kbs/Config/Application.ini.inc');

/**
 * If no cache we use the default ini
 *
 */
$configFile = CONFIG_INC;
$noConfigCache = false;
if (false == is_file(CONFIG_INC)) {
    $configFile = PROJECT_ROOT . '/library/Kbs/Config/Application.ini';
    $noConfigCache = true;
}

/**
 * Zend_Application
 *
 */
require_once 'Zend/Application.php';

/**
 * Create application
 *
 */
$application = new Zend_Application(
    APPLICATION_ENV,
    $configFile
);

/**
 * If no config cache then we create it
 *
 * Only for production
 */
if ($noConfigCache and ('production' == APPLICATION_ENV)) {
    $configs = '<?php' . PHP_EOL 
             . 'return ' 
             . var_export($application->getOptions(), true) . PHP_EOL
             . '?>';
    file_put_contents(CONFIG_INC, $configs);
} else {
    // Debug
    //echo 'The configs are cached';
}

umask(0);

/**
 * Bootstrap and Run
 *
 */
try {
    /**
     * We need to know the warnings when develop
     *
     */
    if ('production' !== APPLICATION_ENV) {
        $application->getAutoloader()->suppressNotFoundWarnings(false);
    }

    /**
     * We only boot Front Controller and leave others loaded on demand
     *
     */
    $application->getBootstrap()->bootstrap('FrontController');
    $application->getBootstrap()->bootstrap('Locale');
    $application->run();

    /**
     * Debug
     */
    //var_dump($application->getBootstrap());

} catch (Exception $e) {

    /*
    if (!include_once(PROJECT_ROOT . '/error/500.html')) {
        @header("HTTP/1.x 500 Internal Server Error");
        @header('Status: 500 Internal Server Error');
    }
    */

    /**
     * Handle all errors by 404
     *
     */
    if (!include_once(PROJECT_ROOT . '/error/404.html')) {
        @header('HTTP/1.x 404 Not Found');
        @header('Status: 404 Not Found');
    }

    if (APPLICATION_ENV != 'production') {
        $errorMessage = get_class($e);

        if ($e->getCode()) {
            $errorMessage .= PHP_EOL . ' (' . $e->getCode() . ') ';
        }

        $errorMessage .= PHP_EOL . $e->getMessage() . PHP_EOL . PHP_EOL;

        $errorMessage .= $e->getTraceAsString() . PHP_EOL;

        $errorMessage .= PHP_EOL . PHP_EOL;

        echo nl2br(print_r($errorMessage, true));
    } else {
        //self::logException($e);
    }

    exit;
}